﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace BRS.BidModule
{
    public partial class Login : Form
    {
        Entities brs = new Entities();
        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                byte[] result = Encoding.Default.GetBytes(this.textBox2.Text.Trim());    // 
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] output = md5.ComputeHash(result);
                string MD5password = BitConverter.ToString(output).Replace("-", "");  // 

                if (textBox1.Text == "admin" && textBox2.Text == "admin")
                {
                    //frmBid bid = new frmBid();
                    //this.Hide();
                    //bid.Show();
                }
                else if (brs.Users.Where(p => p.UserName == textBox1.Text && p.Password == MD5password).Count() > 0)
                {

                    Users loginUser = new Users();

                    loginUser = brs.Users.Where(p => p.UserName == textBox1.Text && p.Password == MD5password).FirstOrDefault();//检验MD5是否一致
                    //MessageBox.Show(loginUser.UserName+loginUser.Password);

                    frmBid bid = new frmBid(loginUser);
                    if (loginUser.Enabled == true)
                    {
                        this.Hide();
                        bid.Show();
                        brs.LoginInfo.Add(new LoginInfo {UserName=loginUser.UserName ,UserId=loginUser.UserId,LoginTime=DateTime.Now ,Client=Dns.GetHostName() });
                        brs.SaveChanges();

                    }
                    else MessageBox.Show("该用户不可用！");


                }
                else MessageBox.Show("用户名或密码错误！");
            }

        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) button1_Click(null, null);
        }
    }
}
